import time

import pika

from utils import get_connection

# 建立socket连接
connection = get_connection()
# credient = pika.PlainCredentials(username='admin', password='admin')
# connection = pika.BlockingConnection(
#     pika.ConnectionParameters(host='0.0.0.0', port=5672, virtual_host='/', credentials=credient))

channel = connection.channel()
channel.exchange_declare(exchange='direct_exchange', exchange_type='direct', durable=True)

for i in range(1000):
    time.sleep(0.5)
    message = f'time {i}'
    if i % 2 == 0:
        channel.basic_publish(
            exchange='direct_exchange',
            routing_key='direct',
            body=message,
            properties=pika.BasicProperties(delivery_mode=2,)  # 确保消息是持久的，设置消息持久化，将要发送的消息的属性标记为2，表示该消息要持久化
        )
    else:
        channel.basic_publish(
            exchange='direct_exchange',
            routing_key='notdirect',
            body=message,
            properties=pika.BasicProperties(delivery_mode=2,)
        )
    print(message)

connection.close()
